/**
 * Created by chenzheyu on 2017/4/28.
 */
$(function () {
    var SEOval = '';
    var limit = 10;
    var companyId = '';
    var companyName = '';
    var CarName = '';
    var vehicleId = '';
    var CarLimitId = '';
    var CarLimitName = '';
    var ShopId = '';
    var ShopName = '';
    var CarSeriesId = '';
    var CarSeriesName = '';
    var CarBrandId = '';
    var CarBrandName = '';
    var purchaseTime = '';
    var plateNo = '';
    var mileage = '';

    var timestamp = Date.parse(new Date()) / 1000;

    $('.purchase input ').datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true,
        endDate: $().Timestamp.TimestampDate(timestamp)
    });
    $('.purchase input ').on('change', function () {

        var Titme = $(this).val();
        purchaseTime = $().Timestamp.DateTimestamp(Titme);
    });
    // $("#endTime").datetimepicker({
    //     format: "yyyy-mm-dd HH:ii",
    //     language: 'zh-CN',
    //     showMeridian: true,
    //     autoclose: true,
    //     todayBtn: true
    // });
    $.ajax({
        url: ctx + '/company/listAllData',
        type: 'GET',
        success: function (data) {

            var DownPullSelectOption = {
                data: [],
                onChange: function (e) {
                    companyId = e.val;
                    companyName = e.text;

                    $.ajax({
                        url: ctx + '/rentshop/selectRentshopByShopName',
                        type: 'GET',
                        data: {
                            companyId: companyId
                        },
                        success: function (data) {
                            var ShopNameDownPullSelectOption = {
                                data: [],
                                onChange: function (e) {
                                    ShopId = e.val;
                                    ShopName = e.text;


                                    $(".vehicleId input").unbind("click");
                                    $('.vehicleId input').on('click', function () {
                                        var vehicleIdOption = {
                                            data: {
                                                Title: '选择车系',
                                                Content: '',
                                                OkChange: function (e, Remove) {
                                                    if (!vehicleId == '') {
                                                        var data = {
                                                            vehicleId: CarSeriesId
                                                        };
                                                        $('.vehicleId input').data('data', data).val(CarSeriesName);
                                                        Remove()
                                                    } else {
                                                        $('body').Overlays({data: {text: '请选择一个车系！', state: 'No'}})
                                                    }
                                                }
                                            },
                                            OKHide: true
                                        };

                                        var vehicleIdContent = $('<div class="vehicleIdContent"></div>');

                                        var pids = ''
                                        $.ajax({
                                            url: ctx + '/rentcontent/selectAllProperty',
                                            type: 'GET',
                                            async: false,
                                            success: function (data) {
                                                for (var i = 0; i < data.length; i++) {
                                                    if (data[i].seq === 4) {
                                                        pids = data[i].id
                                                    }
                                                }
                                            }
                                        });

                                        $.ajax({
                                            url: ctx + '/rentcontent/selectContentByPid',
                                            data: {
                                                pid: pids
                                            },
                                            type: 'GET',
                                            success: function (data) {
                                                console.log(data)
                                                if (data.length > 0) {
                                                    for (var i = 0; i < data.length; i++) {
                                                        (function () {
                                                            var index = i;
                                                            var val = $('<div class="Brand"></div>').html('<div class="BrandName">' + data[index].contentName + '</div><div class="Vehicle CustomClear"></div>');
                                                            var Id = data[index].id;
                                                            var Name = data[index].contentName;
                                                            $.ajax({
                                                                url: ctx + '/rentvehicle/findVehicleByparams',
                                                                data: {
                                                                    shopId: ShopId,
                                                                    brandId: data[index].id
                                                                },
                                                                type: 'GET',
                                                                success: function (data) {
                                                                    ;
                                                                    for (var i = 0; i < data.content.length; i++) {
                                                                        (function () {
                                                                            var index = i;
                                                                            var item = $('<label class="CustomClear"></label>');
                                                                            item.append('<input type="radio" name="Vehicle" value="' + data.content[i].id + '"><span>' + data.content[i].name + '</span>');
                                                                            item.on('click', function () {
                                                                                vehicleId = data.content[index].id;
                                                                                CarSeriesId = data.content[index].id;
                                                                                CarSeriesName = data.content[index].name;
                                                                                CarBrandId = Id;
                                                                                CarBrandName = Name;
                                                                            });
                                                                            val.find('.Vehicle').append(item)
                                                                        })()
                                                                    }

                                                                }
                                                            });
                                                            vehicleIdContent.append(val)
                                                        })()
                                                    }
                                                }
                                            }
                                        });

                                        vehicleIdOption.data.Content = vehicleIdContent;


                                        $('body').ModalBox(vehicleIdOption);

                                    });


                                }
                            };
                            if (data.content.length > 0) {
                                for (var i = 0; i < data.content.length; i++) {
                                    (function () {
                                        var index = i;
                                        var val = {
                                            name: data.content[index].shopName,
                                            Val: data.content[index].id
                                        };
                                        ShopNameDownPullSelectOption.data.push(val)
                                    })()
                                }

                            } else {
                                ShopNameDownPullSelectOption.data.push({name: '该公司下无门店'})
                            }


                            $('.shopName').DownPullSelect(ShopNameDownPullSelectOption);


                        }
                    })


                }
            };
            for (var i = 0; i < data.length; i++) {
                (function () {
                    var index = i;
                    DownPullSelectOption.data.push({
                        name: data[index].abbreviation,
                        Val: data[index].id
                    });
                })()
            }

            $('.companyName').DownPullSelect(DownPullSelectOption);


        }
    });
    var LimitDownPullSelectOption = {
        data: [
            {name: '不限行', Val: 0},
            {name: '星期一', Val: 1},
            {name: '星期二', Val: 2},
            {name: '星期三', Val: 3},
            {name: '星期四', Val: 4},
            {name: '星期五', Val: 5},
            {name: '星期六', Val: 6},
            {name: '星期日', Val: 7}
        ],
        onChange: function (e) {
            CarLimitId = e.val;
            CarLimitName = e.text;
        }
    };
    $('.limit').DownPullSelect(LimitDownPullSelectOption);

    $('.plateNo input').on('blur', function () {
        if ($().isVehicleNumber($(this).val())) {
            $.ajax({
                url: ctx + '/rentcar/findCar',
                data: {
                    param: $(this).val()
                },
                type: 'GET',
                success: function (data) {
                    if (!data.totalElements == 0) {
                        $('body').Overlays({data: {text: "当前车牌号已存在，请重新输入！", state: 'No'}});
                        $('.plateNo input').val('')
                    }
                }
            })
        } else {
            $('body').Overlays({data: {text: "请输入正确的车牌号！", state: 'No'}});
            $('.plateNo input').val('')
        }

    });


    $('.OkBut').on('click', function () {

        if ($('.CarName input').val()) {
            CarName = $('.CarName input').val();
            if ($('.mileage input').val()) {
                mileage = $('.mileage input').val();
                if (!companyId == '' && !companyName == '') {
                    if ($('.purchase input').val()) {
                        if (!ShopId == '' && !ShopName == '') {
                            if (!CarLimitId == '' && !CarLimitName == '') {
                                if ($('.plateNo input').val()) {
                                    plateNo = $('.plateNo input').val();
                                    if (!vehicleId == '') {

                                        var data = {
                                            name: CarName,//车辆名称
                                            companyName: companyName,//公司名称
                                            companyId: companyId,//公司ID
                                            shopId: ShopId,//门店ID
                                            shopName: ShopName,//门店名称
                                            plateNo: plateNo,//车牌号
                                            mileage: mileage,//已行驶里程
                                            limit: CarLimitId,//是否限行 0不限行 1-7对应星期
                                            purchase: purchaseTime,//购置时间
                                            vehicleId: vehicleId//车系ID
                                        };


                                        $.ajax({
                                            url: ctx + '/rentcar/insertCar',
                                            data: data,
                                            type: 'POST',
                                            success: function (data) {

                                                if (data.message) {
                                                    $('body').Overlays({data: {text: '添加车辆成功，即将跳转页面！', state: "Ok"}});
                                                    window.location.href = ctx + '/rentcar/list'
                                                } else {
                                                    $('body').Overlays({data: {text: '添加失败，请稍后重试！'}, state: 'No'});
                                                }
                                            }
                                        });


                                    } else {
                                        $('body').Overlays({data: {text: '请选择车系！', state: "No"}})
                                    }
                                } else {
                                    $('body').Overlays({data: {text: '请输入车牌号！', state: "No"}})
                                }
                            } else {
                                $('body').Overlays({data: {text: '请选择是否限行！', state: "No"}})
                            }
                        } else {
                            $('body').Overlays({data: {text: '请选择门店！', state: "No"}})
                        }
                    } else {
                        $('body').Overlays({data: {text: '请选择购置时间！', state: "No"}})
                    }
                } else {
                    $('body').Overlays({data: {text: '请选择分公司！', state: "No"}})
                }
            } else {
                $('body').Overlays({data: {text: '请输入已行驶里程！', state: "No"}})
            }
        } else {
            $('body').Overlays({data: {text: '请输入车辆名称！', state: "No"}})
        }
    })
    $('.CancelBut').on('click', function () {
        window.location.href = ctx + '/rentcar/list';
    })


    for (var i = 0; i < $('input[type="number"]').length; i++) {
        (function () {
            var index = i;
            $('input[type="number"]').eq(index).on('change', function () {
                if ($(this).val() < 0) {
                    $(this).val(0)
                }
            })
        })()
    }
});